<?php
if(!defined('IN_BSTO')){ die('[ERROR] You cannot load this page directly !!!'); }

/* @author tubaduc <tubaduc@gmail.com> 
 * @copyright (c) Pyco24
 * @since 27.07.11
 * @version 2.0
 * */
Class CategoryController extends baseController {
	private $errorList = array();
	private $tags = array();
	private $catManager = null;
	private $idNext = null;
	private $idPage = 1;
	public $numRow = 5;
	//public $names = array();
	function __construct($registry)
	{
		parent::__construct($registry);
		$this->catManager = $this->registry->db->get('catManager');
	}
	/* @author tubaduc
	 * @input none
	 * @output $bsto (index.php)
	 * */
	public function get(){
		return $this->registry;
	}
	/* @author tubaduc
	 * @input CatID
	 * @output none
	 * */
    public function index($user, $id = null)
    {
    	if(isset($_GET['numRow'])) {
    		$this->numRow = $_GET['numRow'];
    		if ($this->numRow < 5) {
    			$this->numRow = 5;
    		}
    		else if ($this->numRow < 10 && $this->numRow > 5) {
    			$this->numRow = 5;
    		}
    		else if ($this->numRow > 10 && $this->numRow < 15)  {
    			$this->numRow = 10;
    		}
    		else if ($this->numRow > 15) {
    			$this->numRow = 15;
    		}
    	}
    	if ($id == 'all') {
    		$id = 1;
    	}
    	
    	$this->idPage = $id;
    	
    	$allCatLimit = $this->catManager->getCategoryLimit($id, $this->numRow);
    	
    	$allCat = $this->catManager->countCat();
    	
        $this->errorList['eachRowOfCat'] = "";
        
    	$this->errorList['currentPage'] = $id;
    	
    	$allPage = ceil($allCat / $this->numRow);
    	
    	if ($allPage == 0) {
    		$this->errorList['allPage'] = 1;
    	}
    	else {
    		$this->errorList['allPage'] = $allPage;
    	}
    	
    	if (($id + 1) > $allPage) {
    		$this->idNext = $allPage;
    	}
    	else {
    		$this->idNext = $id +1;
    	}
    	$this->errorList['nextPage'] = '../view/'.$this->idNext.'?numRow='.$this->numRow;
    	
    	if (($id - 1)<1) {
    		$this->errorList['backPage'] = '../view/1?numRow='.$this->numRow;
    	}
    	else {
    		$back = $id - 1;
    		$this->errorList['backPage'] = '../view/'.$back.'?numRow='.$this->numRow;
    	}
    	
    	$this->errorList['farleft'] = '../view/1?numRow='.$this->numRow;
    	$this->errorList['farright'] = '../view/'.$allPage.'?numRow='.$this->numRow;
    	
        for ($count = 0 ; $count < count($allCatLimit) ; $count++) {
        	if($count%2 == 0){
        		$class = '';
        	} else {
        		$class = 'alternate-row';
        	}
        	$this->errorList['eachRowOfCat'] = $this->errorList['eachRowOfCat'].
        	'<tr class="'.$class.'">					
					<td>'.$allCatLimit[$count]->get("cid").'</td>
					<td>'.$allCatLimit[$count]->get("name").'</td>
					<td><a href="../../book/show/'.$allCatLimit[$count]->get("cid").'/1">View Books</a></td>
					<td class="options-width">
					<a href="../edit/'.$allCatLimit[$count]->get("cid").' "title="Edit" class="icon-1 info-tooltip"></a>
					<a href="../delete/'.$allCatLimit[$count]->get("cid").'?numRow='.$this->numRow.'"title="Delete" class="icon-2 info-tooltip"></a>
					</td>
			</tr>';
        }
        
        $content = $this->registry->template->getTemplate('vcat', $this->errorList, 'Module');
        
        $tags = array('bstoContentTitle' => 'List of All Categories');
        
        $this->registry->template->setMenuKey('category');
        $output = $this->registry->template->getPageContent('index', 'BSTO ACP - Category Management', $content, $tags);
        echo $output;  
		
    }
    public function checkokie($missing){
   		if($missing['name']!=""){
    		return false;
    	}
   		if($missing['weight']!=""){
    		return false;
    	}
    	if($missing['status']!=""){
    		return false;
    	}
    	return true;
    }

	public function add($user, $id=null){

		$tags = array(
			"name"=>"",
			"weight"=>"",
			"status"=>"",
			'namevalue'=> "",
			'weightvalue'=> "",
			'statusvalue'=> "",
			'bstoContentTitle' => 'Create Category'
			
		);
		
		$method = $_SERVER['REQUEST_METHOD'];
		if(isset($_POST['submit'])){
			foreach($_POST as $key => $value){
				if(trim($value)=="" ){
						$tags[$key]="This field is requied.";
				}			
			}
		}
		$check = $this->checkokie($tags);

		$content =$this->registry->template->getModule('acat',$tags);
        $this->registry->template->setMenuKey('category');
        $output = $this->registry->template->getPageContent('index', 'BSTO ACP - Add Category', $content, $tags);        
        
        if(isset($_POST['submit']) && $check){
       		$name = $_POST['name'];
       		$weight = $_POST['weight'];
       		$status = $_POST['status'];
        	$catMana = $this->registry->db->get('catManager');
        	$catMana->create($name, $weight, $status);
        	header("Location: ../view/all");
        	exit(1);
        }
        else echo $output; 

    }
    public function edit($user, $id){
    	$method = $_SERVER['REQUEST_METHOD'];
    	$catMana = $this->registry->db->get('catManager');
    	$this->registry->db->startConnection();
    	$rows= mysql_query("Select * from category where cid =$id");
    	$this->registry->db->closeConnection();
    	$row=mysql_fetch_array($rows);
    	$oldname = $row['name'];
    	$oldweight = $row['weight'];
    	$oldstatus =$row['status'];		
    	$tags = array(
    		"namevalue" => $oldname,
    		"weightvalue"=>$oldweight,
    		"statusvalue"=>$oldstatus,
    		"name"=>"",
    		"weight"=>"",
    		"status"=>""
    	);
    	
    	if(isset($_POST['submit'])){
			foreach($_POST as $key => $value){
				if(trim($value)=="" ){
						$tags[$key]="This field is requied.";
				}			
			}
		}
		$check = $this->checkokie($tags);
		$content =$this->registry->template->getModule('acat',$tags);
		$tags = array('bstoContentTitle' => 'Modify Category');
        $this->registry->template->setMenuKey('category');
        $output = $this->registry->template->getPageContent('index', 'Edit Category', $content, $tags);        	
        if($method=="POST" && isset($_POST['submit']) && $check){
        	$name = $_POST['name'];
        	$weight = $_POST['weight'];
        	$status = $_POST['status'];
       	 	$cat = new Category($this->registry->db);
       		$cat->set('cid', $id);
       		$cat->init($name, $weight, $status);
       		$cat->save();
       		//=========================
       		header("Location: ../view/all");
       		//khu vuc link sang trang view all category
       		//=========================
        }
        echo $output;  
    }
    /* @author tubaduc
	 * @input CatID
	 * @output none
	 * */
    public function delete($user, $id = null) 
    {
    	
    	$this->catManager->delete($id);
    	header("Location: ../view/".$this->idPage."?numRow=".$_GET['numRow']);
    	//exit();
    }
}
?>
